package com.hzit.sk.controller;

import com.hzit.sk.req.UserLoginReq;
import com.hzit.sk.resp.Response;
import com.hzit.sk.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户控制器,不写具体的业务逻辑
 */
@Controller
@RequestMapping("/user")
public class UserController {

    Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService userService;

    /**
     * 加载登录页面
     * @return
     */
   @RequestMapping("/show")
    public String show(){
        return "login";
    }



    /**
     * 登录接口
     * @param userLoginReq
     * @return
     */
    //TODO 整合 shiro
    @ResponseBody //返回json数据
    @RequestMapping("/login")
    public Response login(UserLoginReq userLoginReq){
        logger.info("接受到登录请求，参数：{}",userLoginReq);
        Response response = new Response();

        //空判断
        if(StringUtils.isEmpty(userLoginReq.getMobile())){
            return response.getFailData("手机号不能为空");
        }

        if(StringUtils.isEmpty(userLoginReq.getPassword())){
            return response.getFailData("密码不能为空");
        }

        try{
            response = userService.userLogin(userLoginReq);
            return response;
        }catch (Exception e){
            logger.error("Exception",e);
            return response.getFailData("系统繁忙");
        }

    }


}
